package com.lingyi.web.controller.wechat;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lingyi.common.config.CommonConfig;
import com.lingyi.common.core.domain.AjaxResult;
import com.lingyi.common.core.domain.R;
import com.lingyi.common.utils.RestTemplateUtil;
import com.lingyi.common.utils.file.FileUploadUtils;
import com.lingyi.common.utils.file.FileUtils;
import com.lingyi.framework.web.service.WechatService;
import com.lingyi.system.domain.MaterialLib;
import com.lingyi.system.domain.vo.MaterialLibVo;
import com.lingyi.system.service.IMaterialLibService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Arrays;

/**
 * @PackageName: com.lingyi.web.controller.wechat
 * @ClassName: MaterialLibController
 * @Description:
 * @Author: czl
 * @Date: 2025/4/19 18:36
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/wechat/materialLib")
public class MaterialLibController {

    private final IMaterialLibService mediaLibService;
    private final WechatService wechatService;

    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file, String type) {
        try {
            // 上传文件路径
            String filePath = CommonConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String originalFilename = file.getOriginalFilename();
            String mediaId = wechatService.uploadMaterial(file.getInputStream(), type, originalFilename);
            MaterialLib materialLib = new MaterialLib();
            materialLib.setMaterialName(originalFilename);
            materialLib.setType(type);
            materialLib.setMediaId(mediaId);
            materialLib.setProfile(fileName);
            materialLib.setFileName(file.getOriginalFilename());
            mediaLibService.save(materialLib);
            return AjaxResult.success(materialLib);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/selectPage")
    public R<?> selectPage(@RequestBody MaterialLibVo search) {
        return R.ok(mediaLibService.selectPage(search));
    }

    @GetMapping("/getMaterial/{materialId}")
    public R<?> getDetail(@PathVariable Long materialId) {
        return R.ok(mediaLibService.getDetailById(materialId));
    }

    @GetMapping("/getAccessToken")
    public R<?> getAccessToken() {
        return R.ok(wechatService.getAccessToken());
    }

    @GetMapping("/remove/{materialIds}")
    public R<?> remove(@PathVariable Long[] materialIds) {
        return R.ok(mediaLibService.removeBatchByIds(Arrays.asList(materialIds)));
    }

    @PostMapping("/allList")
    public R<?> allList(@RequestBody MaterialLibVo search) {
        return R.ok(mediaLibService.allList(search));
    }

    @PostMapping("/getByMaterialId/{materialId}")
    public R<?> getByMaterialId(@PathVariable String materialId) {
        LambdaQueryWrapper<MaterialLib> queryWrapper = Wrappers.lambdaQuery(MaterialLib.class);
        queryWrapper.eq(MaterialLib::getMediaId, materialId).last("limit 1");
        return R.ok(mediaLibService.getOne(queryWrapper));
    }
    
    
}
